﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Windows;
using WaterKit.TcpUdp;
using WaterKit.Base;
using System.Threading;
using System.Collections;
using Auction_Server.Business;

namespace Auction_Server
{
    /// <summary>
    /// 主程序
    /// </summary>
    public partial class App : Application
    {
        /****************************服务器***************************/
        private static ushort server_port = Convert.ToUInt16(AppOpr.GetConfigStrings("server_port"));
        private static ushort server_maxnum = Convert.ToUInt16(AppOpr.GetConfigStrings("server_maxnum"));
        public static TcpServer MySer = new TcpServer(server_port, server_maxnum);

        /****************************拍卖会***************************/

        /// <summary>
        /// 拍卖会开始倒计时时限,单位：分钟
        /// </summary>
        public static int countdownTime = Convert.ToInt32(new DataTable().Compute(AppOpr.GetConfigStrings("countdownTime"), null));

        /// <summary>
        /// 拍卖会确认标的底价时限,单位:分钟
        /// </summary>
        public static int confirmPriceTime = Convert.ToInt32(new DataTable().Compute(AppOpr.GetConfigStrings("confirmPriceTime"), null));

        /// <summary>
        /// 当前拍卖会编号
        /// </summary>
        public static string currentAuctionNo = "";

        /// <summary>
        /// 当前标的
        /// </summary>
        public static targetModel currentTarget = null;

        /// <summary>
        /// 当前标的编号
        /// </summary>
        public static string currentTargetNo = "";

        /// <summary>
        /// 客户端是否可以登录
        /// </summary>
        public static bool clientCanLogin = false;

        /// <summary>
        /// 是否已经确认标的底价
        /// </summary>
        public static bool isConfirmprice = false;

        /// <summary>
        /// 延时拍卖进入下个环节
        /// </summary>
        public static bool canCycle = false;

        /// <summary>
        /// 是否接收登录
        /// </summary>
        public static bool clientCanBid = true;

        /// <summary>
        /// 当前最高价
        /// </summary>
        public static double maxMoney = 0;

        /// <summary>
        /// 当前最高价用户编号
        /// </summary>
        public static string maxMoneyBidUser = "";

        /// <summary>
        /// 延时拍卖累加价格
        /// </summary>
        public static double auctionType2Money = 0;

        /// <summary>
        /// 当前标的全部单位
        /// </summary>
        public static DataTable currentTargetCompany =new DataTable();

        /// <summary>
        /// 今日所有拍卖会
        /// </summary>
        public static DataTable todayAuction = new DataTable();

        /// <summary>
        /// 所有终端
        /// </summary>
        public static Hashtable htTerminal = new Hashtable();

        /// <summary>
        /// 数据字典
        /// </summary>
        public static Hashtable htBidCodeToName = new Hashtable();

        /// <summary>
        /// 客户端需要标的信息(除用户信息),标的开始时生成
        /// </summary>
        public static string userTargetStr = "";

        /// <summary>
        /// 通讯命令类型
        /// </summary>
        public enum cmdType
        {
            /// <summary>
            /// 无效命令
            /// </summary>
            c00,
            /// <summary>
            /// 客户端登录
            /// </summary>
            c01,
            /// <summary>
            /// 登录失败
            /// </summary>
            c02,
            /// <summary>
            /// 登录成功
            /// </summary>
            c03,
            /// <summary>
            /// 竞价
            /// </summary>
            c04,
            /// <summary>
            /// 更新最高价
            /// </summary>
            c05,
            /// <summary>
            /// 竞价失败
            /// </summary>
            c06,
            /// <summary>
            /// 确定标的底价
            /// </summary>
            c07
        }

        /****************************全局事件***************************/
        /// <summary>
        /// 线程启动服务器
        /// </summary>
        public static void startServer()
        {
            try
            {
                Thread myThread = new Thread(new ThreadStart(Listen));
                myThread.Start();
            }
            catch (Exception ex)
            {
                Log4C.WriteErrLog("线程启动服务器", ex);
            }
        }

        /// <summary>
        /// 服务器开启监听
        /// </summary>
        public static void Listen()
        {
            try
            {
                App.MySer.Start();
            }
            catch (Exception ex)
            {
                Log4C.WriteErrLog("服务器开启监听", ex);
            }
        }

        /// <summary>
        /// 发送数据至客户端
        /// </summary>
        /// <param name="eClient">客户端Session</param>
        /// <param name="cmdType">命令类型</param>
        /// <param name="Content">数据内容</param>
        public static void sendData(Session eClient, App.cmdType cmdType, string Content)
        {
            try
            {
                App.MySer.AsyncSend(eClient, cmdType.ToString() + Content);
            }
            catch (Exception ex)
            {
                Log4C.WriteErrLog("服务器发送数据", ex);
            }
        }
    }
}
